package com.jjb.www.ui.activity;

import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.base.BaseDialog;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.shape.view.ShapeTextView;
import com.hjq.widget.layout.WrapRecyclerView;
import com.jjb.www.R;
import com.jjb.www.app.AppActivity;
import com.jjb.www.app.AppAdapter;
import com.jjb.www.http.api.CurriculumSortApi;
import com.jjb.www.http.api.PayTostApi;
import com.jjb.www.http.api.PaymentListApi;
import com.jjb.www.http.model.HttpData;
import com.jjb.www.http.model.HttpList;
import com.jjb.www.http.model.HttpListData;
import com.jjb.www.ui.adapter.PayListAdapter;
import com.jjb.www.ui.dialog.MessageDialog;
import com.jjb.www.ui.fragment.CourseListFragment;
import com.jjb.www.widget.CommonRefreshView;

import java.util.List;

import okhttp3.Call;
/**
 *    author : Android CRG
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/03/01
 *    desc   : 支付记录
 */
public class PaymentListActivity extends AppActivity implements BaseAdapter.OnItemClickListener {
    private CommonRefreshView mRefreshView;
    private PayListAdapter adapter;
    private ShapeTextView mTvTost;
    private String name = "";



    @Override
    protected int getLayoutId() {
        return R.layout.activity_payment_list;
    }

    @Override
    protected void initView() {
        mRefreshView = findViewById(R.id.refreshView);
        mTvTost = findViewById(R.id.tv_tost);
        adapter = new PayListAdapter(getActivity());
        adapter.setOnItemClickListener(this);
        setOnClickListener(mTvTost);
    }

    @Override
    public void onRightClick(View view) {
        startActivity(InvoiceListActivity.class);
    }

    @Override
    public void onClick(View view) {
        if (view==mTvTost){
            new MessageDialog.Builder(getActivity())
                    .setMessage(name)
                    .setConfirm(R.string.common_confirm)
                    .setCancel(null)
                    .setCancelable(false)
                    .setListener(dialog -> {
                    })
                    .show();
        }
    }

    @Override
    protected void initData() {
        getMessage();
        getListData();

    }

    private void getListData() {
        mRefreshView.setDataHelper(new CommonRefreshView.DataHelper<PaymentListApi.Bean>() {
            @Override
            public AppAdapter<PaymentListApi.Bean> getAdapter() {
                return adapter;
            }

            @Override
            public void loadData(int p, HttpCallback<HttpListData<PaymentListApi.Bean>> callback) {
                EasyHttp.get(PaymentListActivity.this)
                        .api(new PaymentListApi()
                                .setPage(p)
                                .setPageSize(adapter.getSize())
                        ).request(new HttpCallback<HttpListData<PaymentListApi.Bean>>(PaymentListActivity.this) {

                    @Override
                    public void onStart(Call call) {

                    }

                    @Override
                    public void onSucceed(HttpListData<PaymentListApi.Bean> result) {
                        callback.onSucceed(result);
                    }

                    @Override
                    public void onFail(Exception e) {
                        super.onFail(e);
                        callback.onFail(e);
                    }

                    @Override
                    public void onEnd(Call call) {
                        super.onEnd(call);
                        callback.onEnd(call);
                    }
                });
            }

            @Override
            public void onRefreshSuccess(List<PaymentListApi.Bean> list, int listCount) {

            }

            @Override
            public void onRefreshFailure() {

            }

            @Override
            public void onLoadMoreSuccess(List<PaymentListApi.Bean> loadItemList, int loadItemCount) {

            }

            @Override
            public void onLoadMoreFailure() {

            }
        });
        mRefreshView.initData();
    }

    private void getMessage() {
        EasyHttp.get(this)
                .api(new PayTostApi())
                .request(new HttpCallback<HttpData<PayTostApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<PayTostApi.Bean> data) {
                        name = data.getData().getName();
                    }

                });
    }

    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        Intent intent = new Intent();
        intent.putExtra("id",adapter.getItem(position).getId()+"");
        intent.setClass(getContext(),PayDetailsActivity.class);
        startActivity(intent);
    }
}